#基类Base
#显示等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time

class Base():
    #定义初始化方法
    def __init__(self,driver):
        self.driver = driver
    #浏览器方法
    def goto(self,url):
        self.driver.get(url)


    #定位元素
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(
                lambda driver: self.driver.find_element(*loc).is_displayed
            )
            return self.driver.find_element(*loc)
        except Exception as e:
            #时间戳
            t = time.strftime('%Y%m%d_%H%M%S')
            #时间+截图
            self.driver.save_screenshot('yichang.png')
            print(e)
    #点击事件
    def click(self,loc):
        self.find_element(loc).click()
    #输入事件
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    #文本事件
    def text(self,loc):
        return print(self.find_element(loc).text)
    #清空事件
    def clear(self,loc):
        self.find_element(loc).clear()
    #截图
    #键盘事件：

    #回车
    def Enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    #页面切换
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #滚动——任意位置
    def scrollto_x(self,x):
        self.driver.execute_script(f'scrollTo(0,{x})')
    #滚动——底部
    def scrollto_Height(self):
        self.driver.execute_script('scrollTo(0,document.body.scrollToHeight)')
    #框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    #弹框——accept()
    def aleer_accept(self):
        alert = self.driver.switch_to.alert
        alert.accept()
    #弹框——dismiss()
    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()
    #鼠标事件
    #双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #三击
    def Three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()
    #悬停
    def move_to(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    #拖拽
    # def drop_and_drop(self,loc,x,y):
    #     ActionChains(self.driver).drag_and_drop(self.find_element(loc),).perform()
    #按住不放
    def click_and_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()

    #键盘
    #全选
    def Crtl_a(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')
    #剪切
    def Crtl_x(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'x')
    #复制
    def Ctrl_c(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    #粘贴
    def Ctrl_v(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')
    #Tab时间
    def Tab(self,loc):
        self.find_element(loc).send_keys(Keys.TAB)
    #退格事件
    def BACK_SPACE(self,loc):
        self.find_element(loc).send_keys(Keys.BACK_SPACE)
    #空格事件'
    def SPACE(self,loc):
        self.find_element(loc).send_keys(Keys.SPACE)

    #下拉框——value
    def Select_value(self,loc,info):
        targe = Select(self.find_element(loc))
        targe.select_by_value(info)
    #下拉框——index
    def Select_index(self,loc,x):
        targe = Select(self.find_element(loc))
        targe.select_by_index(x)

    #下拉框——visible_text
    def Select_vivible_text(self,loc,info):
        targe = Select(self.find_element(loc))
        targe.select_by_visible_text(info)



